#include <pthread.h>
#include <stdio.h>
#include <stdbool.h>
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int item_count = 0;
void* consumer_p(void*)
{
    while(true)
    {
        pthread_mutex_lock(&mutex);
        item_count += 1;
        printf("生产, 总量: %d\n", item_count);
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
void* consumer_f(void*)
{
    while(true)
    {
        pthread_mutex_lock(&mutex);
        item_count -= 1;
        printf("消费,总量:%d\n", item_count);
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
int main()
{
    // pthread_mutex_init(&mutex, NULL);
    pthread_t th1, th2;
    pthread_attr_t th_attr;
    pthread_attr_init(&th_attr);
    pthread_attr_setdetachstate(&th_attr, PTHREAD_CREATE_DETACHED);
    int ret = pthread_create(&th1, &th_attr, consumer_p, NULL);
    if(0 != ret)
    {
        perror("create consumer_p");
        pthread_mutex_destroy(&mutex);
        return 1;
    }
    ret = pthread_create(&th2, &th_attr, consumer_f, NULL);
    if(0 != ret)
    {
        perror("create consumer_f");
        pthread_mutex_destroy(&mutex);
        return 1;
    }
    pthread_attr_destroy(&th_attr);
    while(true);
    pthread_mutex_destroy(&mutex);
    return 0;
}
